package zy.data.food.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import zy.data.food.dto.OrderDTO;
import zy.data.food.enums.ResultEnum;
import zy.data.food.exception.SellException;
import zy.data.food.service.OrderService;

import java.util.Map;

/**
 * @Auther: zhangy
 * @Date: 2018/9/10 13:36
 * @Description: 卖家端订单
 */
@Controller
@RequestMapping(value = "/seller/order")
@Slf4j
public class SellerOrderController {

        @Autowired
        private OrderService orderService;

        //查询所有
        @GetMapping(value = "/list")
        public ModelAndView list(@RequestParam(value = "page",defaultValue = "0") Integer page,
                                 @RequestParam(value = "size",defaultValue = "5") Integer size,
                                 Map<String,Object> map){
            PageRequest pageRequest = new PageRequest(page,size);
            Page<OrderDTO> orderDTOPage = orderService.findList(pageRequest);

            map.put("orderDTOPage",orderDTOPage);
            map.put("currentPage",page);
            map.put("size",size);

            //调用模板
            return new ModelAndView("order/list",map);
        }

        //取消订单
        @GetMapping(value = "/cancle")
        public ModelAndView cancle(@RequestParam(value = "orderId") String orderId,
                                   Map<String,Object> map){

            try {
                OrderDTO orderDTO = orderService.findone(orderId);
                orderService.cancle(orderDTO);

            }catch (SellException e){

                log.error("【卖家端取消订单】发生异常");
                map.put("msg",e.getMessage());
                map.put("url","/sell/seller/order/list");
                return new ModelAndView("common/error",map);
            }

            map.put("msg", ResultEnum.SUCCESS.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/success",map);
        }

        //订单详情查看
        @GetMapping(value = "/detail")
        public ModelAndView detail(@RequestParam(value = "orderId") String orderId,
                                   Map<String,Object> map){

            OrderDTO orderDTO = new OrderDTO();
            try {
                 orderDTO = orderService.findone(orderId);

            }catch (SellException e){

                log.error("【卖家端查询订单详情】发生异常{}",e);
                map.put("msg",e.getMessage());
                map.put("url","/sell/seller/order/list");
                return new ModelAndView("common/error",map);
            }
            map.put("orderDTO",orderDTO );
            return new ModelAndView("order/detail",map);

        }

        //完结订单
        @GetMapping("/finish")
        public ModelAndView finished(@RequestParam(value = "orderId") String orderId,
                                     Map<String,Object> map){

        try {
            OrderDTO orderDTO = orderService.findone(orderId);
            orderService.finish(orderDTO);

        }catch (SellException e){

            log.error("【卖家端完结订单】发生异常{}",e);
            map.put("msg",e.getMessage());
            map.put("url","/sell/seller/order/list");
            return new ModelAndView("common/error",map);
        }

        map.put("msg", ResultEnum.SUCCESS.getMessage());
        map.put("url","/sell/seller/order/list");
        return new ModelAndView("common/success",map);


        }


}


















